package inteligencia_mapa;



import TDADiccionario.Entry;
import TDAListaSE.Iterator;
import TDAListaSE.NoSuchElementException;
import roles.Bala;
import roles.Malo;
import main.Programa;

public class HiloParaBala extends Thread{

	protected Programa elPrograma;
	protected Bala miBala;
	
	public HiloParaBala(Programa mp, Bala b){
		elPrograma = mp;
		miBala = b;
	}
	
	public boolean eliminarMalo(){
		Iterator<Entry<Integer,Malo>> it = elPrograma.obtenerMalos();
		int coordXbala = miBala.obtenerCoordenada().obtenerX();
		int coordYbala = miBala.obtenerCoordenada().obtenerY();
		boolean toR = false;
		while(it.hasNext()){
			try{
				Entry<Integer,Malo> aux = it.next();
				int coordXmalo = aux.getValue().obtenerCoordenada().obtenerX();
				int coordYmalo = aux.getValue().obtenerCoordenada().obtenerY();
				boolean chocaXDerecha = coordXbala + 32 == coordXmalo && coordYbala<coordYmalo+32 && coordYbala>coordYmalo-32;
				if(chocaXDerecha){
					aux.getValue().matar();
					elPrograma.obtenerMovMapa().obtenerContainer().remove(aux.getValue().obtenerRepresentacionGrafica());
					elPrograma.eliminarMalo(aux);
					toR = true;
					miBala.eliminar();
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					this.stop();
				}
			}catch (NoSuchElementException e){
				e.printStackTrace();
			}
		}
		return toR;
		
		
	}
	
	
	public void run(){
		try {
			this.join(10);
		} catch (InterruptedException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while(true){
			try{
				if(!eliminarMalo()){
					if(miBala.obtenerMovPJ().puedo_ir_derecha())
						miBala.irDerecha();
					else{
						miBala.eliminar();
						Thread.sleep(100);
						this.stop();
					}
				}
				Thread.sleep(65);
					
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
}
